<template>
  <view>
    <view>
      <view class="home_tab">
        <view class="home_tab_title">
          <view class="title_inner">
            <uni-segmented-control
              :current="current"
              :values="items.map((v) => v.title)"
              @clickItem="onClickItem"
              styleType="text"
              activeColor="#d4237a"
            ></uni-segmented-control>
          </view>
          <view class="iconfont icon-search"></view>
        </view>
        <view class="home_tab_content">
          <view v-show="current === 0">
            <home-recommend></home-recommend>
          </view>
          <view v-show="current === 1">
            <home-category></home-category>
          </view>
          <view v-show="current === 2"> <home-new></home-new> </view>
          <view v-show="current === 3"> <home-album></home-album> </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import homeAlbum from "./home-album";
import homeCategory from "./home-category";
import homeNew from "./home-new";
import homeRecommend from "./home-recommend";

import { uniSegmentedControl } from "@dcloudio/uni-ui";

export default {
  data() {
    return {
      title: "首页",
      items: [
        {
          title: "推荐",
        },
        {
          title: "分类",
        },
        {
          title: "最新",
        },
        {
          title: "专辑",
        },
      ],
      current: 0,
    };
  },
  onLoad() {
    this.request({
      url:"http://service.picasso.adesk.com/v3/homepage/vertical"
    })
    .then(res=>{
      // console.log(res);
    });
    //   http://service.picasso.adesk.com/v3/homepage/vertical
    //原生的请求
    // wx.request({
    //   url: "http://service.picasso.adesk.com/v3/homepage/vertical",
    //   success(res) {
    //     console.log("wx.request");
    //     console.log(res);
    //   },
    // });
    // uni.request({
    //   url: "http://service.picasso.adesk.com/v3/homepage/vertical", //仅为示例，并非真实接口地址。
    //   data: {
    //     text: "uni.request",
    //   },
    //   header: {
    //     "custom-header": "hello", //自定义请求头信息
    //   },
    //   success: (res) => {
    //     console.log("uniapp.request");
    //     console.log(res);
    //     // this.text = 'request success';
    //   },
    // });
  },
  methods: {
    onClickItem(e) {
      if (this.current != e.currentIndex) {
        this.current = e.currentIndex;
        uni.setNavigationBarTitle({
          title: this.items[e.currentIndex].title
        });
      }
    },
  },
  components: {
    // TODO 组件和页面的关系，加载的顺序，四个组件是在页面加载的时候就创建了?
    // 组件内 mounted 中设置uni.setNavigationBarTitle({，只显示最后专辑的title,这是为什么，我的解决方法是修改到methods-onClickItem中实现了
    homeCategory,
    homeRecommend,
    homeAlbum,
    homeNew,
    uniSegmentedControl,
  },
};
</script>

<style lang="scss">
.home_tab{
  .home_tab_title{
    position: relative;
    .title_inner{
      width: 60%;
      margin: 0 auto;
    }
    .icon-search{
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      right: 5%;
      font-size: 45rpx;
      // color: $uni-color-primary;
    }
  }
}
</style>
